var UUID = Java.type("java.util.UUID")
/**
 * 创建表头
 */
function createData(){
    var list = new ArrayList()
    var creditCardSize = context.get(keys.CRED_CARD_SIZE) == null ? 0 : Integer.parseInt(context.get(keys.CRED_CARD_SIZE));
    for(var i = 0; i < creditCardSize; i++) {
        var data = new  CreditCardAllData()
        data.setBankName(bankNameMapping[context.get("bankName")]);
        data.setCardType("信用卡");
        data.setCardId(UUID.randomUUID().toString());
        var bills = createBillsHeaderFromStartTimeToEndTime("CREDITCARD");
        bills.add(createUNDONEBillHeader());
        data.setBills(bills)
        var creditCardTimeDeposit = new ArrayList();
        data.setDeposits(creditCardTimeDeposit);
        list.add(data)
    }

    var debitCardSize = context.get(keys.DEBT_CARD_SIZE) == null ? 0 : Integer.parseInt(context.get(keys.DEBT_CARD_SIZE));
    for(var i = 0; i < debitCardSize; i++) {
        var data = new CreditCardAllData()
        data.setBankName(bankNameMapping[context.get("bankName")]);
        data.setCardType("借记卡");
        data.setCardId(UUID.randomUUID().toString());
        var bills = createBillsHeaderFromStartTimeToEndTime("DEBITCARD");
        data.setBills(bills)
        var creditCardTimeDeposit = new ArrayList();
        data.setDeposits(creditCardTimeDeposit);
        list.add(data)
    }
    context.put(ALL_DATA,list)
}

/**
 * 创建信用卡数据对象
 */
function createCreditData(){
    var list = new ArrayList()
    var creditCardSize = context.get(keys.CRED_CARD_SIZE) == null ? 0 : Integer.parseInt(context.get(keys.CRED_CARD_SIZE));
    for(var i = 0; i < creditCardSize; i++) {
        var data = new  CreditCardAllData()
        data.setBankName(bankNameMapping[context.get("bankName")]);
        data.setCardType("信用卡");
        data.setCardId(UUID.randomUUID().toString());
        var bills = createBillsHeaderFromStartTimeToEndTime("CREDITCARD");
        bills.add(createUNDONEBillHeader());
        data.setBills(bills)

        var creditCardTimeDeposit = new ArrayList();
        data.setDeposits(creditCardTimeDeposit);
        list.add(data)
    }
    context.put(keys.CREDIT_DATA,list)
}

/**
 * 创建借记卡数据对象
 */
function createDebitData(){
    try {
        var list = new ArrayList();
        var debitCardSize = context.get(keys.DEBT_CARD_SIZE) == null ? 0 : Integer.parseInt(context.get(keys.DEBT_CARD_SIZE));
        for (var i = 0; i < debitCardSize; i++) {
            var data = new CreditCardAllData();
            data.setBankName(bankNameMapping[context.get("bankName")]);
            data.setCardType("借记卡");
            data.setCardId(UUID.randomUUID().toString());
            var bills = createBillsHeaderFromStartTimeToEndTime("DEBITCARD");
            bills.add(createUNDONEBillHeader());
            data.setBills(bills)

            var creditCardTimeDeposit = new ArrayList();
            data.setDeposits(creditCardTimeDeposit);
            list.add(data)
        }
        context.put(keys.DEBIT_DATA, list);
    } catch (e) {
        logger.error("创建借记卡数据对象异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
    }
}

/**
 *　根据账单周期长度创建表头信息
 */
function createBillsHeaderFromStartTimeToEndTime(cardType) {
    //获取银行
    var bankname = context.get("bankName");
    //入口标识
    var entrance = context.get("entrance");
    if (StringUtils.isBlank(bankname)) {
        throw new BankException("BALO-99601-05", "未能找到相应的bankName");
    } else if (StringUtils.isBlank(cardType)) {
        throw new BankException("BALO-99601-05", "未能找到相应的卡类型");
    } else if (StringUtils.isBlank(entrance)) {
        throw new BankException("BALO-99601-05", "未能找到相应的入口信息");
    }
    logger.info("开始获取银行时间集合 bankName【{}】,cardType【{}】,entrance【{}】",bankname,cardType,entrance);
    var timeUnit = conf[bankname][cardType][entrance]["timeUnit"];
    var time = conf[bankname][cardType][entrance]["time"];
    if (timeUnit == "M") {
        time = Integer.parseInt(time);
    } else if(timeUnit == "Y"){
        time = Integer.parseInt(time)*12;
    }
    var bills = new ArrayList();
    for(var i = 0; i < time; i++ ){
        var  billAllData = new BillAllData();
        billAllData.setBillId(UUID.randomUUID().toString());
        if(equals(cardType,"DEBITCARD")){
            billAllData.setBillType("");
        }else{
            billAllData.setBillType("DONE");
        }
        billAllData.setBankName(bankNameMapping[context.get("bankName")]);
        //billAllData.setBillMonth(months[i]);
        billAllData.setShoppingSheets(new ArrayList());
        billAllData.setInstallments(new ArrayList());
        bills.add(billAllData);
    }
    return bills
}

function createUNDONEBillHeader(){
    var  billAllData = new BillAllData();
    billAllData.setBillId(UUID.randomUUID().toString());
    billAllData.setBillType("UNDONE");
    billAllData.setBankName(bankNameMapping[context.get("bankName")]);
    //billAllData.setBillMonth(months[i]);
    billAllData.setShoppingSheets(new ArrayList());
    billAllData.setInstallments(new ArrayList());
    return billAllData;

}

/**
 * 获取一个已出账单的内容为空的billData
 */
function getDoneBillAllDataHeaderData(creditCardAllData) {
    var bills = creditCardAllData.getBills();
    var billAllData = null;
    for(var i=0;i<bills.length;i++){
        // print(bills[i].getBillType())
        if ((bills[i].getBillType().equals("DONE") || bills[i].getBillType().equals("")) && bills[i].getShoppingSheets().size() <= 0 && bills[i].getBillMonth() == null ){
            billAllData = bills[i];
            return billAllData;
        }
    }
    throw new BankException("BALO-99601-05", "未能找到可用的已出账单billAllData");
}
/**
 * 获取一个未出账单的内容为空的billData
 */
function getUNDoneBillAllDataHeaderData(creditCardAllData) {
    var bills = creditCardAllData.getBills();
    var billAllData = null;
    for(var i=0;i<bills.length;i++){
        if (bills[i].getBillType().equals("UNDONE")){
            billAllData = bills[i];
            return billAllData
        }
    }
    throw new BankException("BALO-99601-05", "未能找到可用的未出账单billAllData");
}


/**
 * 向有内容的下一个已出账单billData中设置内容
 * @param month
 * @param list
 */
function setDoneBillAllDataByMonth(billAllData,month, slist,ilist) {
    //拿到有内容的下一个空表头的billAllData
    billAllData.setBillMonth(month)
    billAllData.getShoppingSheets().addAll(slist)
    billAllData.getInstallments().addAll(ilist);
    return billAllData;
}
/**
 * 向有内容的下一个未出账单billData中设置内容
 * @param month
 * @param list
 */
function setUNDoneBillAllDataByMonth(billAllData,month, slist,ilist) {
    //拿到有内容的下一个空表头的billAllData
    billAllData.setBillMonth(month)
    billAllData.getShoppingSheets().addAll(slist)
    billAllData.getInstallments().addAll(ilist);
    return billAllData;
}


/**
 * 比较两个字符串日期的大小
 */
function compireStrDate(date1,date2) {
    var d1year = Integer.parseInt(date1.substring(0,4));
    var d1mon = Integer.parseInt(date1.substring(5,7));
    var d2year = Integer.parseInt(date2.substring(0,4));
    var d2mon = Integer.parseInt(date2.substring(5,7));
    if (d1year > d2year) {
        return true;
    } else if (d1year < d2year) {
        return false;
    } else {
        if (d1mon > d2mon) {
            return true;
        } else {
            return false;
        }
    }
}



